home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_07 / 9n07098a < prev    next >
Text File  |  1991-05-26  |  10KB  |  343 lines

  1.  
  2.  
  3. /*
  4.  *      xlib2.c
  5.  *      A  more advanced X Window 
  6.  *      (Xlib)-based program using text,
  7.  *      written for the C Users Journal.
  8.  *
  9.  *      Link with the X library, e.g.,
  10.  *
  11.  *              cc -o xlib2 xlib2.c -lX11
  12.  *
  13.  *      Define SYSV if you have malloc()
  14.  *      declared in stdlib.h.
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19.  
  20. #ifdef SYSV
  21. #include <stdlib.h>
  22. #endif
  23.  
  24. #include  <X11/Xlib.h>
  25. #include  <X11/Xutil.h>
  26. #include  <X11/keysymdef.h>
  27. #include  <X11/keysym.h>
  28.  
  29.  
  30. /*
  31.  *      We have a hard-coded size and location
  32.  */
  33. #define X_LOCATION              10
  34. #define Y_LOCATION              20
  35. #define WIDTH                   400
  36. #define HEIGHT                  50
  37. #define MAX_STRING_LENGTH       400
  38.  
  39. /*
  40.  *      Use xlsfonts to find a font name
  41.  *      that is available on your system.
  42.  */
  43. /* #define FONT_NAME               "fixed"  */
  44. #define FONT_NAME               "variable"
  45.  
  46.  
  47. main( argc, argv )
  48.  
  49. int     argc;
  50. char    *argv[];
  51.  
  52. {       /* main */
  53.         Display         *display;
  54.         int             screen;
  55.         Window          rootwindow;
  56.         Window          window;
  57.         XSizeHints      *sizehints;
  58.         XWMHints        *wmhints;
  59.         GC              gc;
  60.         XEvent          event;
  61.         int             done;
  62.         char            string[ MAX_STRING_LENGTH + 4 ];
  63.         XFontStruct     *font;
  64.  
  65.  
  66.         /*
  67.          * Connect to an X server.
  68.          */
  69.         display = XOpenDisplay( (char *) NULL );
  70.  
  71.         if ( display == (Display *) NULL )
  72.                 {
  73.                 fprintf( stderr, "Error opening display\n" );
  74.                 exit( 1 );
  75.                 }
  76.  
  77.         screen     = DefaultScreen( display );
  78.         rootwindow = RootWindow( display, screen );
  79.  
  80.         /*
  81.          * Create a window
  82.          */
  83.         window = XCreateSimpleWindow( display,
  84.                         rootwindow,     /* parent */
  85.                         X_LOCATION, Y_LOCATION,
  86.                         WIDTH, HEIGHT,
  87.                         1,              /* border width */
  88.                         BlackPixel( display, screen ),
  89.                         WhitePixel( display, screen ) );
  90.  
  91.     /*
  92.      * Set up Window Manager Hints for keyboard input
  93.      */
  94.         wmhints = (XWMHints *) malloc( sizeof(XWMHints) );
  95.  
  96.     wmhints->flags = InputHint;
  97.     wmhints->input = True;
  98.  
  99.     XSetWMHints(display, window, wmhints );
  100.  
  101.     free( wmhints );
  102.  
  103.         /*
  104.          * Set up hints about the window
  105.          */
  106.         sizehints = (XSizeHints *) malloc( sizeof(XSizeHints) );
  107.  
  108.         sizehints->x      = X_LOCATION;
  109.         sizehints->y      = Y_LOCATION;
  110.         sizehints->width  = WIDTH;
  111.         sizehints->height = HEIGHT;
  112.         sizehints->flags  = PPosition | PSize;
  113.  
  114.         /* 
  115.          * Use XSetWMProperties() in R4
  116.          *      
  117.          */
  118.         XSetStandardProperties( display, window,
  119.                 "Xlib2",        /* window name */
  120.                 "Xlib2",        /* icon name */
  121.                 (Pixmap) None,  /* Icon pixmap */
  122.                 argv, argc,
  123.                 sizehints );
  124.  
  125.         free( sizehints );
  126.  
  127.     
  128.         /*
  129.          * Ask for Expose, mouse (Button) and keyboard input
  130.          */
  131.         XSelectInput( display, window,
  132.                 ButtonPressMask | ExposureMask | KeyPressMask );
  133.  
  134.  
  135.         /*
  136.          * Load up a font. 
  137.          */
  138.         font = XLoadQueryFont( display, FONT_NAME );
  139.  
  140.  
  141.         if ( font == (XFontStruct *) NULL )
  142.                 {
  143.                 fprintf( stderr, "Error in loading %s font.\n",
  144.                         FONT_NAME );
  145.  
  146.                 XCloseDisplay( display );
  147.                 exit( 1 );
  148.                 }
  149.  
  150.  
  151.         /*
  152.          * Create a Graphics Context (GC) for drawing text
  153.          */
  154.         gc = XCreateGC( display, window,
  155.                 0L, (XGCValues *) NULL );
  156.  
  157.         XSetForeground( display, gc, 
  158.                 BlackPixel( display, screen ) );
  159.  
  160.         /*
  161.          * Set the background color, too
  162.          */     
  163.         XSetBackground( display, gc, 
  164.                 WhitePixel( display, screen ) );
  165.  
  166.         /*
  167.          * Set the GC to draw in the given font.
  168.          */
  169.         XSetFont( display, gc, font->fid );
  170.  
  171.  
  172.         /*
  173.          * Make Window appear on the screen
  174.          */
  175.         XMapWindow( display, window );
  176.         XFlush( display );
  177.  
  178.         string[0] = '\0'; /* null out string */
  179.         
  180.         done = False;
  181.  
  182.         while( !done )
  183.                 {
  184.                 XNextEvent( display, &event );
  185.  
  186.                 switch( event.type )
  187.                         {
  188.                         case ButtonPress:
  189.                                 XCloseDisplay( display );
  190.  
  191.                                 exit( 0 );
  192.                                 break;
  193.                         case Expose:
  194.                                 /*
  195.                                  * Only redraw when all
  196.                                  * Expose events are in
  197.                                  */
  198.                                 if ( event.xexpose.count == 0 )
  199.                                         {
  200.                                         RedrawText( display, window, 
  201.                                                 gc, string );
  202.                                         }
  203.                                 break;
  204.                         case KeyPress:
  205.                                 EditString( display, window,
  206.                                         &event, string, 
  207.                                         MAX_STRING_LENGTH );
  208.  
  209.                                 RedrawText( display, window, 
  210.                                         gc, string );
  211.                                 break;
  212.                         }
  213.  
  214.                 }
  215.  
  216.  
  217. }       /* main */
  218.  
  219. RedrawText( display, window, gc, string )
  220.  
  221. Display *display;
  222. Window  window;
  223. GC      gc;
  224. char    string[];
  225.  
  226. {       /* RedrawText */
  227.  
  228.         XDrawImageString( display, window, gc,
  229.                 5, 20,          /* location */
  230.                 string,
  231.                 strlen( string ) );
  232.  
  233. #define MESSAGE "Click a Mouse Button to Exit"
  234.  
  235.         XDrawImageString( display, window, gc,
  236.                 5, 40,          /* location */
  237.                 MESSAGE,
  238.                 strlen( MESSAGE ) );
  239.  
  240.         XFlush( display );
  241.  
  242. }       /* RedrawText */
  243.  
  244. EditString( display, window, event, string, max_length )
  245.  
  246. Display         *display;
  247. Window          window;
  248. XKeyEvent       *event;
  249. char            string[];
  250. int             max_length;
  251.  
  252. {       /* EditString */
  253.         int             length;
  254.         char            new_string[ 12 ];
  255.         KeySym          keysym;
  256.         XComposeStatus  composestatus;
  257.  
  258.         length = XLookupString( event,
  259.                         new_string,
  260.                         1,              /* max length of new_string */
  261.                         &keysym,
  262.                         &composestatus );
  263.  
  264.     new_string[1] = '\0';   /* some systems add garbage to new_string */
  265.  
  266.         if ( ( event->state & Mod1Mask ) || ( event->state & Mod2Mask ) )
  267.                 {
  268.                 /*
  269.                  * ALT key
  270.                  */
  271.                 }
  272.  
  273.         if ( event->state & ShiftMask )
  274.                 {
  275.                 /*
  276.                  * Shift key down
  277.                  */
  278.                 }
  279.  
  280.         /*
  281.          * This is a US ASCII test, so you may need to change this.
  282.          */
  283.         if ( ( length > 0 ) && ( keysym > 31 ) && ( keysym < 127 ) )
  284.                 {
  285.                 if ( ( length + strlen( string ) ) < max_length ) 
  286.                         {
  287.                         strcat( string, new_string );
  288.                         }
  289.                 }
  290.         else
  291.                 {
  292.                 /*
  293.                  * Could be a function or special key.
  294.                  * We'll only check for a few here.
  295.                  */
  296.                 switch( keysym )
  297.                         {
  298.                         case XK_Return: /* Return key */
  299.